package com.example.cpl.myapplication;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class View4 extends FragmentActivity {
    private static final String TAG = "TAG";
    private Button btn_save1,btn_clear1,btn_save2,btn_clear2,btn_save3,btn_clear3,btn_save4,btn_clear4;
    private int t1,t2,t3,t4;
    private TextView tv_out1,tv_out2,tv_out3,tv_out4;
    private EditText et_in1,et_in2,et_in3,et_in4;
    private String check_zs,check_zw,check_xw,check_ws;
    String filename1_1 = "MySharedString_vp1_1",filename2_1 = "MySharedString_vp2_1",filename3_1 = "MySharedString_vp3_1",filename4_1= "MySharedString_vp4_1";
    String filename1_2 = "MySharedString_vp1_2",filename2_2 = "MySharedString_vp2_2",filename3_2 = "MySharedString_vp3_2",filename4_2= "MySharedString_vp4_2";
    String filename1_3 = "MySharedString_vp1_3",filename2_3 = "MySharedString_vp2_3",filename3_3 = "MySharedString_vp3_3",filename4_3= "MySharedString_vp4_3";

    private String change(int a)
    {
        switch (a) {                         //    1表示早上 2表示中午 。。。。。
            case 1:
                switch (MainActivity.who) {
                    case 1:
                        return filename1_1;
                    case 2:
                        return filename1_2;
                    case 3:
                        return filename1_3;
                }
                 break;

            case 2:
                switch(MainActivity.who)
                {
                    case 1: return filename2_1;
                    case 2: return filename2_2;
                    case 3: return filename2_3;
                }
                break;

            case 3:
                switch(MainActivity.who)
                {
                    case 1: return filename3_1;
                    case 2: return filename3_2;
                    case 3: return filename3_3;
                }
                break;
            case 4:

                switch(MainActivity.who)
                {
                    case 1: return filename4_1;
                    case 2: return filename4_2;
                    case 3: return filename4_3;
                }
                break;

        }

        return null;

    }


    private TabLayout tab1;
    private ViewPager vpg1;
    public List<View> viewList;
    private View view1, view2, view3,view4;
//    private ViewPager mViewPager;
    private static final String[] DATA = {"早上", "中午", "下午", "晚上","凌晨"};

    public String read_SP_Str(String s,String s1,String file) {
        final SharedPreferences someData = getSharedPreferences(file, 0);
        return  someData.getString(""+s+"",""+s1+"");
    }
    public int read_SP_Int(String s,int s1,String file) {
        final SharedPreferences someData = getSharedPreferences(file, 0);
        return  someData.getInt(""+s+"",s1);
    }
    public void  write_SP_Str(String s,String s1,String file) {
        final SharedPreferences someData = getSharedPreferences(file, 0);
        SharedPreferences.Editor editor = someData.edit();      //让文件 可以编辑
        editor.putString(""+s+"",""+s1+"");
        editor.commit();// 提交修改
    }
    public void  write_SP_Int(String s,int s1,String file) {
        final SharedPreferences someData = getSharedPreferences(file, 0);
        SharedPreferences.Editor editor = someData.edit();      //让文件 可以编辑
        editor.putInt(""+s+"",s1);
        editor.commit();// 提交修改
    }
    public void clear_SP(String file) {
        final SharedPreferences someData = getSharedPreferences(file, 0);
        SharedPreferences.Editor editor = someData.edit();
        editor.clear();
        editor.commit();

    }
    public String getTime(){
        long time=System.currentTimeMillis();   //long now = android.os.SystemClock.uptimeMillis();
        SimpleDateFormat format=new SimpleDateFormat("MM-dd HH:mm");
        Date d1=new Date(time);
        String t1=format.format(d1);
        Log.d("TAG",t1);
        return t1;
    }
    private void setSP() {
        int i1,i2,i3,i4;
        t1= read_SP_Int("参数", 0,change(1));
        t2= read_SP_Int("参数", 0,change(2));
        t3= read_SP_Int("参数", 0,change(3));
        t4= read_SP_Int("参数", 0,change(4));
//
//        Log.d("TAG","读取的参数t1="+""+t1+"");
//        Log.d("TAG","读取的参数t2="+""+t2+"");
//        Log.d("TAG","读取的参数t3="+""+t3+"");
//        Log.d("TAG","读取的参数t4="+""+t4+"");
        tv_out1.setText("");        //清空历史数据
        tv_out2.setText("");
        tv_out3.setText("");
        tv_out4.setText("");
        for (i1 =1; i1 <= t1 ; i1++) {
            String sg_data = read_SP_Str("早餐" + "" + i1 + "", "",change(1));
            String time_data = read_SP_Str("时间" + "" + i1 + "", "",change(1));

            tv_out1.setText(tv_out1.getText() + "记录时间 "+""+time_data+""+"\n"+sg_data + "\n\n");
//            Log.d("TAG", "早上吃的是 = " + "" + sg_data + "" + "参数=" + "" + i1 + "");
        }
        for (i2 =1; i2 <= t2 ; i2++) {

            String sg_data = read_SP_Str("午餐" + "" + i2 + "", "",change(2));
            String time_data = read_SP_Str("时间" + "" + i2 + "", "",change(2));
            tv_out2.setText(tv_out2.getText() + "记录时间 "+""+time_data+""+"\n"+sg_data + "\n\n");
            Log.d("TAG", "中午吃的是 = " + "" + sg_data + "" + "参数=" + "" + i2 + "");

        }
        for (i3 =1; i3 <= t3 ; i3++) {
            String sg_data = read_SP_Str("晚餐" + "" + i3 + "", "",change(3));
            String time_data = read_SP_Str("时间" + "" + i3 + "", "",change(3));
            tv_out3.setText(tv_out3.getText() + "记录时间 "+""+time_data+""+"\n"+sg_data + "\n\n");
            Log.d("TAG", "下午吃的是 = " + "" + sg_data + "" + "参数=" + "" + i3 + "");

        }
        for (i4 =1; i4 <= t4 ; i4++) {
            String sg_data = read_SP_Str("夜宵" + "" + i4 + "", "",change(4));
            String time_data = read_SP_Str("时间" + "" + i4 + "", "",change(4));
            tv_out4.setText(tv_out4.getText() + "记录时间 "+""+time_data+""+"\n"+sg_data + "\n\n");
            Log.d("TAG", "晚上吃的是 = " + "" + sg_data + "" + "参数=" + "" + i4 + "");

        }
    }
    @SuppressLint("WrongViewCast")
    private void initWidght() {

        LayoutInflater inflater=getLayoutInflater();

        view1 = inflater.inflate(R.layout.activity_vp1,null);
        view2 = inflater.inflate(R.layout.activity_vp2,null);
        view3 = inflater.inflate(R.layout.activity_vp3,null);
        view4 = inflater.inflate(R.layout.activity_vp4,null);
        viewList= new ArrayList<View>();

        tab1= (TabLayout) findViewById(R.id.tab1);
        vpg1 = (ViewPager) findViewById(R.id.vpg1);

        tv_out1 = (TextView)view1.findViewById(R.id.tv_out1);
        et_in1 = (EditText)view1.findViewById(R.id.et_in1);
        btn_save1 = (Button)view1.findViewById(R.id.btn_save1);
        btn_clear1 = (Button)view1.findViewById(R.id.btn_clear1);

        tv_out2 = (TextView)view2.findViewById(R.id.tv_out2);
        et_in2 = (EditText)view2.findViewById(R.id.et_in2);
        btn_save2 = (Button)view2.findViewById(R.id.btn_save2);
        btn_clear2 = (Button)view2.findViewById(R.id.btn_clear2);

        tv_out3 = (TextView)view3.findViewById(R.id.tv_out3);
        et_in3 = (EditText)view3.findViewById(R.id.et_in3);
        btn_save3 = (Button)view3.findViewById(R.id.btn_save3);
        btn_clear3 = (Button)view3.findViewById(R.id.btn_clear3);

        tv_out4 = (TextView)view4.findViewById(R.id.tv_out4);
        et_in4 = (EditText)view4.findViewById(R.id.et_in4);
        btn_save4 = (Button)view4.findViewById(R.id.btn_save4);
        btn_clear4 = (Button)view4.findViewById(R.id.btn_clear4);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view4);

        initWidght();

        tv_out1.setMovementMethod(ScrollingMovementMethod.getInstance());       //设置历史数据滚动
        tv_out2.setMovementMethod(ScrollingMovementMethod.getInstance());
        tv_out3.setMovementMethod(ScrollingMovementMethod.getInstance());
        tv_out4.setMovementMethod(ScrollingMovementMethod.getInstance());
        setSP();

        btn_save1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String zs=null;
                zs=et_in1.getText().toString();
                if(!zs.equals("")){
                    if(!zs.equals(check_zs)) {

                        t1 = t1 + 1;
                        String time=getTime();
                        tv_out1.setText(tv_out1.getText().toString()+ "记录时间 "+""+time+""+"\n早餐：" + "" +  et_in1.getText().toString()  + ""+"\n\n");
                        /**********滚动到更新的一行*********/
                        int offset=tv_out1.getLineCount()*tv_out1.getLineHeight();
                        if(offset>tv_out1.getHeight()){
                            tv_out1.scrollTo(0,offset-tv_out1.getHeight());
                        }
                        /**********滚动到更新的一行*********/
                        write_SP_Str("时间" + "" + t1 + "", time,change(1));
                        write_SP_Str("早餐" + "" + t1 + "", "早餐：" + et_in1.getText().toString(),change(1));
                        write_SP_Int("参数", t1,change(1));

                        Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT)
                                .show();//阴影提示框

                        check_zs = et_in1.getText().toString();

                        Log.d("TAG", "保存的时间= " + "" + time + "" + "  参数= " + "" + t1 + "");
                        Log.d("TAG", "保存的早餐= " + "" + et_in1.getText().toString() + "" + "  参数= " + "" + t1 + "");
                        Log.d("TAG", "SP存储名：时间" + "" + t1 + "");
                        Log.d("TAG", "SP存储名：早餐" + "" + t1 + "");
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "已保存", Toast.LENGTH_SHORT)
                                .show();//阴影提示框
                    }
                }
                else
                {
                    AlertDialog dlg = new AlertDialog.Builder(View4.this)
//                            .setIcon(R.drawable.icon)     //加入提示框图标
                            .setTitle("提示")
                            .setMessage("请输入早餐！")
                            .setPositiveButton("确定", null)
                            .create();
                    dlg.show();
                    Log.d("TAG", "请输入早餐！");
                }



            }});


        btn_clear1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clear_SP(change(1));
                t1=0;
//                check_sg=null;
//                check_tz=null;
                tv_out1.setText("");
                et_in1.setText("");

                Toast.makeText(getApplicationContext(), "已清空", Toast.LENGTH_SHORT)
                        .show();//阴影提示框
            }
        });
        btn_save2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String zw=null;
                zw=et_in2.getText().toString();
                if(!zw.equals("")){
                    if(!zw.equals(check_zw)) {

                        t2 = t2 + 1;
                        String time=getTime();
                        tv_out2.setText(tv_out2.getText().toString()+ "记录时间 "+""+time+""+"\n午餐：" + "" +  et_in2.getText().toString()  + ""+"\n\n");
                        /**********滚动到更新的一行*********/
                        int offset=tv_out2.getLineCount()*tv_out2.getLineHeight();
                        if(offset>tv_out2.getHeight()){
                            tv_out2.scrollTo(0,offset-tv_out2.getHeight());
                        }
                        /**********滚动到更新的一行*********/
                        write_SP_Str("时间" + "" + t2 + "", time,change(2));
                        write_SP_Str("午餐" + "" + t2 + "", "午餐：" + et_in2.getText().toString(),change(2));
                        write_SP_Int("参数", t2,change(2));
                        Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT)
                                .show();//阴影提示框
                        check_zw = et_in2.getText().toString();
                        Log.d("TAG", "保存的时间= " + "" + time + "" + "  参数= " + "" + t2 + "");
                        Log.d("TAG", "保存的午餐= " + "" + et_in2.getText().toString() + "" + "  参数= " + "" + t2 + "");
                        Log.d("TAG", "SP存储名：时间" + "" + t2 + "");
                        Log.d("TAG", "SP存储名：午餐" + "" + t2 + "");
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "已保存", Toast.LENGTH_SHORT)
                                .show();//阴影提示框
                    }
                }
                else
                {
                    AlertDialog dlg = new AlertDialog.Builder(View4.this)
//                            .setIcon(R.drawable.icon)     //加入提示框图标
                            .setTitle("提示")
                            .setMessage("请输入午餐！")
                            .setPositiveButton("确定", null)
                            .create();
                    dlg.show();
                    Log.d("TAG", "请输入午餐！");
                }
            }});
        btn_clear2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clear_SP(change(2));
                t2=0;

                tv_out2.setText("");
                et_in2.setText("");
                Toast.makeText(getApplicationContext(), "已清空", Toast.LENGTH_SHORT)
                        .show();//阴影提示框
            }
        });

        btn_save3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String xw=null;
                xw=et_in3.getText().toString();
                if(!xw.equals("")){
                    if(!xw.equals(check_xw)) {

                        t3 = t3 + 1;
                        String time=getTime();
                        tv_out3.setText(tv_out3.getText().toString()+ "记录时间 "+""+time+""+"\n晚餐：" + "" +  et_in3.getText().toString()  + ""+"\n\n");
                        /**********滚动到更新的一行*********/
                        int offset=tv_out3.getLineCount()*tv_out3.getLineHeight();
                        if(offset>tv_out3.getHeight()){
                            tv_out3.scrollTo(0,offset-tv_out3.getHeight());
                        }
                        /**********滚动到更新的一行*********/
                        write_SP_Str("时间" + "" + t3 + "", time,change(3));
                        write_SP_Str("晚餐" + "" + t3 + "", "晚餐：" + et_in3.getText().toString(),change(3));
                        write_SP_Int("参数", t3,change(3));
                        Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT)
                                .show();//阴影提示框
                        check_xw = et_in3.getText().toString();



                        Log.d("TAG", "保存的时间= " + "" + time + "" + "  参数= " + "" + t3 + "");
                        Log.d("TAG", "保存的晚餐= " + "" + et_in3.getText().toString() + "" + "  参数= " + "" + t3 + "");
                        Log.d("TAG", "SP存储名：时间" + "" + t3 + "");
                        Log.d("TAG", "SP存储名：晚餐" + "" + t3 + "");
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "已保存", Toast.LENGTH_SHORT)
                                .show();//阴影提示框
                    }
                }
                else
                {
                    AlertDialog dlg = new AlertDialog.Builder(View4.this)
//                            .setIcon(R.drawable.icon)     //加入提示框图标
                            .setTitle("提示")
                            .setMessage("请输入晚餐！")
                            .setPositiveButton("确定", null)
                            .create();
                    dlg.show();
                    Log.d("TAG", "请输入晚餐！");
                }



            }});
        btn_clear3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clear_SP(change(3));
                t3=0;

                tv_out3.setText("");
                et_in3.setText("");
                Toast.makeText(getApplicationContext(), "已清空", Toast.LENGTH_SHORT)
                        .show();//阴影提示框
            }
        });

        btn_save4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ws=null;
                ws=et_in4.getText().toString();
                if(!ws.equals("")){
                    if(!ws.equals(check_ws)) {

                        t4 = t4 + 1;
                        String time=getTime();
                        tv_out4.setText(tv_out4.getText().toString()+ "记录时间 "+""+time+""+"\n夜宵：" + "" +  et_in4.getText().toString()  + ""+"\n\n");
                        /**********滚动到更新的一行*********/
                        int offset=tv_out4.getLineCount()*tv_out4.getLineHeight();
                        if(offset>tv_out4.getHeight()){
                            tv_out4.scrollTo(0,offset-tv_out4.getHeight());
                        }
                        /**********滚动到更新的一行*********/
                        write_SP_Str("时间" + "" + t4 + "", time,change(4));
                        write_SP_Str("夜宵" + "" + t4 + "", "夜宵：" + et_in4.getText().toString(),change(4));
                        write_SP_Int("参数", t4,change(4));
                        Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT)
                                .show();//阴影提示框
                        check_ws = et_in4.getText().toString();



                        Log.d("TAG", "保存的时间= " + "" + time + "" + "  参数= " + "" + t4 + "");
                        Log.d("TAG", "保存的夜宵= " + "" + et_in4.getText().toString() + "" + "  参数= " + "" + t4 + "");
                        Log.d("TAG", "SP存储名：时间" + "" + t4 + "");
                        Log.d("TAG", "SP存储名：夜宵" + "" + t4 + "");
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "已保存", Toast.LENGTH_SHORT)
                                .show();//阴影提示框
                    }
                }
                else
                {
                    AlertDialog dlg = new AlertDialog.Builder(View4.this)
//                            .setIcon(R.drawable.icon)     //加入提示框图标
                            .setTitle("提示")
                            .setMessage("请输入夜宵！")
                            .setPositiveButton("确定", null)
                            .create();
                    dlg.show();
                    Log.d("TAG", "请输入夜宵！");
                }



            }});


        btn_clear4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clear_SP(change(4));
                t4=0;

                tv_out4.setText("");
                et_in4.setText("");
                Toast.makeText(getApplicationContext(), "已清空", Toast.LENGTH_SHORT)
                        .show();//阴影提示框
            }
        });


        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        viewList.add(view4);
//        MyPageAdapter adapter = new MyPageAdapter(viewList);

//        tab1.setTabMode(TabLayout.MODE_SCROLLABLE);
//        tab1.setTabsFromPagerAdapter(adapter);
        // 设置Tab的选择监听
        tab1.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {   //监听viewpager滑动
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 每当我们选择了一个Tab就将ViewPager滚动至对应的Page
                vpg1.setCurrentItem(tab.getPosition(), true);
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        // 构造一个TabLayout OnPageChangeListener对象
        TabLayout.TabLayoutOnPageChangeListener listener = new TabLayout.TabLayoutOnPageChangeListener(tab1);   //?
        vpg1.addOnPageChangeListener(listener);// 设置ViewPager页面改变后的监听


        PagerAdapter pagerAdapter = new PagerAdapter() {
            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;
            }
            @Override
            public int getCount() {
                return viewList.size();
            }
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(viewList.get(position));
            }
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(viewList.get(position));
                return viewList.get(position);
            }
        };


        vpg1.setAdapter(pagerAdapter);// 设置ViewPager的适配器
    }

//    public class TabPagerAdapter extends PagerAdapter {
//        @Override
//        public int getCount() {
//            return 4; //标签数
//        }
//
//        @Override
//        public boolean isViewFromObject(View view, Object object) {
//            return view == object;
//        }
//
//        @Override
//        public Object instantiateItem(ViewGroup container, int position) {
////            EditText et =new EditText(View4.this);
////            Button   btn=new Button(View4.this);
//            TextView tv = new TextView(View4.this);
//            Log.d("TAG","instantiateItem = "+""+position+"");
//            switch(position)
//            {
//                case 0 :   tv.setText("现在是早上");break;
//                case 1 :   tv.setText("现在是中午");break;
//                case 2 :   tv.setText("现在是下午");break;
//                case 3 :   tv.setText("现在是晚上");break;
//
//            }
////            tv.setText(DATA[position]);
//
//            tv.setGravity(Gravity.CENTER);
//            container.addView(tv,ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.WRAP_CONTENT);
//            return tv;
//        }
//
//        @Override
//        public void destroyItem(ViewGroup container, int position, Object object) {
//            container.removeView((View) object);
//            Log.d("TAG","destroyItem = "+""+position+"");
//        }
//
//        @Override
//        public CharSequence getPageTitle(int position) {
//            return DATA[position];
//        }
//    }
//    public class MyPageAdapter extends PagerAdapter{
//        private List<View> viewList;
//        public MyPageAdapter(List<View> viewList)
//        {
//            this.viewList = viewList;
//        }
//
//        @Override
//
//        public int getCount() {
//            return viewList.size();
//        }
//
//        @Override
//        public boolean isViewFromObject(View view, Object object) {
//            return view == object;
//        }
//
//
//
//    @Override
//        public Object instantiateItem(ViewGroup container, int position) {
//            container.addView(viewList.get(position));
//
//            return viewList.get(position);
//        }
//
//        @Override
//        public void destroyItem(ViewGroup container, int position, Object object) {
//            //super.destroyItem(container, position, object);
//
//            //container.removeView(viewList.get(position));
//        }
//    }
}

